
layui.use(['jquery','layer','form','table','laydate','upload'],function(){
    var $ = layui.$,layer = layui.layer,form = layui.form,table = layui.table,laydate = layui.laydate,upload = layui.upload;
    var orderStatus = 7;
    var orderType = 2;

    //普同检索 22/2/21 谭
    form.on('submit(sub)',function(data){
        if (orderType == 2){
            $.ajax({
                url: '/order/getCourseOrders'
                ,data: {
                    'orderPaymentChannel': data.field.orderPaymentChannel
                    ,'orderSource': data.field.orderSource
                    ,'orderStatus': 7
                    ,'orderType': 2
                }
                ,dataType: 'json'
                ,success:function(data){
                    tableRenderCourse(data.data);
                }
            });
        } else if (orderType == 3){
            $.ajax({
                url: '/order/getGoodsOrders'
                ,data: {
                    'orderPaymentChannel': data.field.orderPaymentChannel
                    ,'orderSource': data.field.orderSource
                    ,'orderStatus': 7
                    ,'orderType': 3
                }
                ,dataType: 'json'
                ,success:function(data){
                    tableRender(data.data);
                }
            });
        }
    });

    //按钮切换 22/2/21 谭
    $(".btn").on("click","button",function () {
        $(this).removeClass("layui-btn-primary").siblings("button").addClass("layui-btn-primary");
    });
    $(".btn").on("mouseover","button",function () {
        $(this).css("border-color","#d2d2d2");
    });

    //表格点击事件
    table.on('row(layuiTbale)',function (data) {
        localStorage.setItem("courseOrderData",JSON.stringify(data.data));
    });

    //课程订单 22/2/21 谭
    $('#unpaid').click(function(){
        orderStatus = 7;
        orderType = 2;
        getUsersCourse(orderStatus,orderType);
    });
    //商品订单 22/2/21 谭
    $('#paid').click(function () {
        orderStatus = 7;
        orderType = 3;
        getUsers(orderStatus,orderType);
    });

//获取课程退订列表 22/2/21 谭
    getUsersCourse(7,2);

    function getUsersCourse(orderStatus,orderType){
        $.ajax({
            url: '/order/getCourseOrders'
            ,data: {
                'orderStatus': orderStatus,
                'orderType': orderType,
            }
            ,dataType: 'json'
            ,success:function(data){
                //渲染表格
                tableRenderCourse(data.data);
            }
        });
    }
    //获取商品退订列表 22/2/21 谭
    function getUsers(orderStatus,orderType){
        $.ajax({
            url: '/order/getGoodsOrders'
            ,data: {
                'orderStatus': orderStatus,
                'orderType': orderType,
            }
            ,dataType: 'json'
            ,success:function(data){
                //渲染表格
                tableRender(data.data);
            }
        });
    }
//渲染表格 22/2/21 谭
    function tableRenderCourse(data){
        table.render({
            elem: '#layuiTbale'
            ,data: data
            ,toolbar: '#toolutil'
            ,page: {
                layout: ['count', 'prev', 'page', 'next', 'limit', 'refresh', 'skip']//自定义分页布局
                ,limits:[10,15,20]
                ,first: true
                ,last: true
            }
            ,limit: 10
            ,cols: [[
                {checkbox: true}
                ,{type: 'numbers',title: '序号',align: 'center'}
                ,{field: 'nickname',title: '用户昵称',align: 'center'}
                ,{field: 'phone',title: '用户手机',align: 'center'}
                ,{field: 'orderType',title: '订单类型',align: 'center',templet:function(data){
                        if(data.orderType == 3){
                            return '商品'
                        }else if(data.orderType == 2){
                            return '课程'
                        }else{
                            return '数据错误';
                        }
                    }}
                ,{field: 'orderStatus',title: '订单状态',align: 'center',templet:function(data){
                        if(data.orderStatus == 2){
                            return '未付款'
                        }else if(data.orderStatus == 3){
                            return '已付款'
                        }else{
                            return '数据错误';
                        }
                    }}
                ,{field: 'orderSource',title: '订单来源',align: 'center',templet:function(data){
                        if(data.orderSource == 2){
                            return '小程序';
                        }else if(data.orderSource == 3){
                            return 'app';
                        }
                    }}
                ,{field: 'courseName',title: '课程名称',align: 'center'}
                ,{field: 'courseIntro',title: '课程描述',align: 'center'}
                ,{field: 'orderPaymentChannel',title: '支付渠道',align: 'center',templet:function(data){
                        if(data.orderPaymentChannel == 2){
                            return '微信支付';
                        }else if(data.orderPaymentChannel == 3){
                            return '余额支付'
                        }
                    }}
                ,{field: 'userId',title: '主讲教师',align: 'center',templet:function(data){
                        //查询主讲师
                        var trueName = '';
                        $.ajax({
                            url: '/order/getUserById'
                            ,async: false
                            ,data: {'userId': data.userId}
                            ,dataType: 'json'
                            ,success:function(data){
                                trueName =  data.data.trueName;
                            }
                        });
                        return trueName;
                    }}
                ,{title: '操作',align: 'center',templet:function(data){
                        return '<a onclick="see('+data.orderId+','+ data.orderType +')" style="color: #009688;">查看</a> ' +
                            '<a onclick="del('+data.orderId+','+ data.orderType +')" style="color: #009688;">删除</a> '
                    }}
            ]]
        });
    }

    function tableRender(data){
        table.render({
            elem: '#layuiTbale'
            ,data: data
            ,toolbar: '#toolutil'
            ,page: {
                layout: ['count', 'prev', 'page', 'next', 'limit', 'refresh', 'skip']//自定义分页布局
                ,limits:[10,15,20]
                ,first: true
                ,last: true
            }
            ,limit: 10
            ,cols: [[
                {checkbox: true}
                ,{type: 'numbers',title: '序号',align: 'center'}
                ,{field: 'nickname',title: '用户昵称',align: 'center'}
                ,{field: 'phone',title: '用户手机',align: 'center'}
                ,{field: 'orderType',title: '订单类型',align: 'center',templet:function(data){
                        if(data.orderType == 3){
                            return '商品'
                        }else if(data.orderType == 2){
                            return '课程'
                        }else{
                            return '数据错误';
                        }
                    }}
                ,{field: 'orderStatus',title: '订单状态',align: 'center',templet:function(data){
                        if(data.orderStatus == 7){
                            return '申请退订'
                        }else{
                            return '数据错误';
                        }
                    }}
                ,{field: 'orderSource',title: '订单来源',align: 'center',templet:function(data){
                        if(data.orderSource == 2){
                            return '小程序';
                        }else if(data.orderSource == 3){
                            return 'app';
                        }
                    }}
                ,{field: 'productName',title: '商品名称',align: 'center'}
                ,{field: 'productDescription',title: '商品描述',align: 'center'}
                ,{field: 'orderPaymentChannel',title: '支付渠道',align: 'center',templet:function(data){
                        if(data.orderPaymentChannel == 2){
                            return '微信支付';
                        }else if(data.orderPaymentChannel == 3){
                            return '余额支付'
                        }
                    }}
                ,{title: '商家名称',align: 'center',templet:function(data){
                        return '思云';
                    }}
                ,{title: '操作',align: 'center',templet:function(data){
                        return '<a onclick="see('+data.orderId+','+ data.orderType +')" style="color: #009688;">查看</a> ' +
                            '<a onclick="del('+data.orderId+','+ data.orderType +')" style="color: #009688;">删除</a> '
                    }}
            ]]
        });
    }



//查看 22/2/21 谭
    see = function see(orderId,orderType){
        if (orderType == 2){
            layer.open({
                type: 2
                ,title: '课程订单详情信息'
                ,area: ['1600px','900px']
                ,content: '/order/course/popup/courseOrder-list-see.html'
                ,success: function (layero,index) {
                    var courseOrderData = JSON.parse(localStorage.getItem("courseOrderData"));
                    var objData = layero.find("iframe").contents();
                    //订单编号
                    objData.find("#orderLogisticsNumber").text(courseOrderData.orderLogisticsNumber);
                    //订单状态
                    var orderStatus = courseOrderData.orderStatus;
                    if (orderStatus == 2){
                        objData.find("#orderStatus").text("未付款");
                    }else if (orderStatus == 3){
                        objData.find("#orderStatus").text("已付款");
                    } else{
                        objData.find("#orderStatus").text("数据错误");
                    }
                    //订单类型
                    var orderType = courseOrderData.orderType;
                    if (orderType == 2){
                        objData.find("#orderType").text("视频");
                    } else if (orderType == 3){
                        objData.find("#orderType").text("商品");
                    }
                    //订单来源
                    var orderSource = courseOrderData.orderSource;
                    if (orderSource == 2){
                        objData.find("#orderSource").text("小程序");
                    } else if (orderSource == 3){
                        objData.find("#orderSource").text("app");
                    }
                    //订单支付方式
                    var orderPaymentChannel = courseOrderData.orderPaymentChannel;
                    if (orderPaymentChannel == 2){
                        objData.find("#orderPaymentChannel").text("微信支付");
                    } else if (orderPaymentChannel == 3){
                        objData.find("#orderPaymentChannel").text("余额支付");
                    }
                    //订单价格
                    objData.find("#coursePrice").text(courseOrderData.coursePrice);
                    //优惠金额
                    objData.find("#couponPrice").text(courseOrderData.coursePrice - courseOrderData.courseNprice);
                    //实付金额
                    objData.find("#courseNprice").text(courseOrderData.courseNprice);
                    //用户账号
                    objData.find("#account").text(courseOrderData.account);
                    //用户昵称
                    objData.find("#nickname").text(courseOrderData.nickname);
                    //用户手机
                    objData.find("#phone").text(courseOrderData.phone);
                    //用户邮箱
                    objData.find("#email").text(courseOrderData.email);
                    //课程名称
                    objData.find("#courseName").text(courseOrderData.courseName);
                    //课程分类
                    objData.find("#className").text(courseOrderData.title);
                    //课程时长
                    objData.find("#courseTime").text(courseOrderData.courseTime);
                    //课程详情
                    objData.find("#courseInfo").text(courseOrderData.courseInfo);
                    //课程图片
                    if (courseOrderData.url != ''){
                        objData.find("#url").attr("src",courseOrderData.url);
                    }
                    //课程视频地址
                    objData.find("#courseUrl").attr("src",courseOrderData.courseUrl);
                }
            })
        }else if (orderType == 3){
            layer.open({
                type: 2
                ,title: '商品订单详情信息'
                ,area: ['1600px','900px']
                ,content: '/order/goods/popup/goodsOrder-list-see.html'
                ,success: function (layero,index) {
                    var goodsOrderData = JSON.parse(localStorage.getItem("goodsOrderData"));
                    var objData = layero.find("iframe").contents();
                    //订单编号
                    objData.find("#orderLogisticsNumber").text(goodsOrderData.orderLogisticsNumber);
                    //订单状态
                    var orderStatus = goodsOrderData.orderStatus;
                    if (orderStatus == 2){
                        objData.find("#orderStatus").text("未付款");
                    }else if (orderStatus == 3){
                        objData.find("#orderStatus").text("已付款");
                    } else{
                        objData.find("#orderStatus").text("数据错误");
                    }
                    //订单类型
                    var orderType = goodsOrderData.orderType;
                    if (orderType == 2){
                        objData.find("#orderType").text("视频");
                    } else if (orderType == 3){
                        objData.find("#orderType").text("商品");
                    }
                    //订单来源
                    var orderSource = goodsOrderData.orderSource;
                    if (orderSource == 2){
                        objData.find("#orderSource").text("小程序");
                    } else if (orderSource == 3){
                        objData.find("#orderSource").text("app");
                    }
                    //订单支付方式
                    var orderPaymentChannel = goodsOrderData.orderPaymentChannel;
                    if (orderPaymentChannel == 2){
                        objData.find("#orderPaymentChannel").text("微信支付");
                    } else if (orderPaymentChannel == 3){
                        objData.find("#orderPaymentChannel").text("余额支付");
                    }
                    //商品价格
                    objData.find("#originalpriceProduct").text(goodsOrderData.originalpriceProduct);
                    //优惠金额
                    objData.find("#couponPrice").text(goodsOrderData.originalpriceProduct - goodsOrderData.currentpriceProduct);
                    //实付金额
                    objData.find("#currentpriceProduct").text(goodsOrderData.currentpriceProduct);
                    //用户账号
                    objData.find("#account").text(goodsOrderData.account);
                    //用户昵称
                    objData.find("#nickname").text(goodsOrderData.nickname);
                    //用户手机
                    objData.find("#phone").text(goodsOrderData.phone);
                    //用户邮箱
                    objData.find("#email").text(goodsOrderData.email);
                    //商品名称
                    objData.find("#productName").text(goodsOrderData.productName);
                    //商品分类
                    objData.find("#title").text(goodsOrderData.title);
                    //服务保证
                    var ensure = goodsOrderData.ensure;
                    if (ensure == 2){
                        objData.find("#ensure").text("退货无忧");
                    }else if (ensure == 3){
                        objData.find("#ensure").text("快速退款");
                    } else if (ensure == 4){
                        objData.find("#ensure").text("包邮");
                    }
                    //商品描述
                    objData.find("#productDescription").text(goodsOrderData.productDescription);
                    //商品图片
                    if (goodsOrderData.mainImage != ''){
                        objData.find("#mainImage").attr("src",goodsOrderData.mainImage);
                    }
                }
            })
        }
    }

//删除 22/2/21 谭
    del = function del(orderId,orderType){
        layer.open({
            title: '删除订单'
            ,content: '确定删除此订单吗'
            ,btn: ['确定','取消']
            ,yes:function(index){
                //删除请求
                $.ajax({
                    url: '/order/delOrder'
                    ,data: {'orderId': orderId}
                    ,dataType: 'json'
                    ,success:function(data){
                        layer.close(index);
                        if (orderType == 2) {
                            getUsersCourse(orderStatus,2);
                        }else if (orderType == 3){
                            getUsers(orderStatus,3);
                        }
                        layer.msg('已删除');
                    }
                });
            }
            ,btn2:function(index){
                layer.close(index);
            }
        })
    }


//刷新 22/2/8  赵
    refresh = function refresh(){
        location.reload();
    }

//返回 22/2/8  赵



});
//返回首页
function backOff(){
    parent.jumpSystemHome();
}